package com.zr.controller;

/**
 * @auther zhaorui
 * @date 2019-11-22 - 0:20
 */

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.zr.constant.MessageConstant;
import com.zr.entity.PageResult;
import com.zr.entity.QueryPageBean;
import com.zr.entity.Result;
import com.zr.pojo.CheckItem;
import com.zr.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 检查项操作
 */
@RestController
@RequestMapping("/checkItem")
@Slf4j
public class CheckItemController {



    @Reference //到zookeeper注册服务中心查找 服务
    private CheckItemService checkItemService;


    /**
     *  增加检查项
     */
    @PreAuthorize("hasAuthority('CHECKITEM_ADD')")
    @RequestMapping("/add")
    //解析 前端传回的 json数据   将它封装为指定的CheckItem对象
    public Result add(@RequestBody CheckItem checkItem){
        try {
            checkItemService.add(checkItem);
            return Result.ok(MessageConstant.ADD_CHECKGROUP_SUCCESS, null);
        } catch (Exception e) {
            log.error(MessageConstant.ADD_CHECKGROUP_FAIL + ", checkItem is:{}", JSON.toJSONString(checkItem), e);
            return Result.ok(MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        /*
            queryPageBean 包括 :
                private Integer currentPage;//页码
                private Integer pageSize;//每页记录数
                private String queryString;//查询条件
         */
        PageResult pageResult = checkItemService.pageQuery(queryPageBean);
        return pageResult;
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        try {
            checkItemService.deleteById(id);
            return Result.ok(MessageConstant.DELETE_CHECKGROUP_SUCCESS,null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.DELETE_CHECKGROUP_FAIL);
        }
    }




    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            CheckItem checkItem = checkItemService.findById(id);
            return Result.ok(MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }


    @PreAuthorize("hasAuthority('CHECKITEM_EDIT')")
    @RequestMapping("/edit")
    public Result edit(@RequestBody CheckItem checkItem){
        try {
           checkItemService.edit(checkItem);
            return Result.ok(MessageConstant.EDIT_CHECKITEM_SUCCESS,null);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try {
            List<CheckItem> checkItemList = checkItemService.findAll();
            return Result.ok(MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);

        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }
}
